package com.paschino.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.paschino.modal.Category;

@Repository
public class CategoryDAOImpl extends AbstractDAO implements CategoryDAO {

	@Override
	public boolean addCategory(Category category) {
		getSession().saveOrUpdate(category);
		if(category.getId() > 0){
			return true;
		}
		return false;
	}

	@Override
	public Category getCategory(int id) {
		return (Category) getSession().get(Category.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> getCategorieList() {
		List<Category> categoryList = new ArrayList<Category>();
		Query query = getSession().createQuery("from Category c where (c.isDelete is null or c.isDelete = false)");
		categoryList = query.list();
		return categoryList;
	}

	@Override
	public boolean updateCategory(Category category) {
		String cateName = category.getName();
		getSession().update(category);
		if(cateName.equals(category.getName())){
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Category> getCategoryListByActive(){
		List<Category> categoryList = new ArrayList<Category>();
		Query query = getSession().createQuery("from Category c where (c.isDelete is null or c.isDelete = false) and c.isActive = true");
		categoryList = query.list();
		return categoryList;
	}

	@Override
	public int deleteCategory(int cateID){
		Query query = getSession().createQuery("update Category c set c.isDelete = true where c.id = :cateID");
		query.setParameter("cateID", cateID);
		int result = query.executeUpdate();
		return result;
	}
	
	@Override
	public int hideCategory(int cateID){
		Query query = getSession().createQuery("update Category c set c.isActive = false where c.id = :cateID");
		query.setParameter("cateID", cateID);
		int result = query.executeUpdate();
		return result;
	}
	
	@Override
	public int unhideCategory(int cateID){
		Query query = getSession().createQuery("update Category c set c.isActive = true where c.id = :cateID");
		query.setParameter("cateID", cateID);
		int result = query.executeUpdate();
		return result;
	}

}
